package handling.opcode;

import handling.ExternalCodeTableGetter;
import handling.WritableIntValueHolder;

import java.io.*;
import java.util.Properties;

public enum RecvPacketOpcode implements WritableIntValueHolder {
    PONG(0x97, false),
    CLIENT_AUTH(0x66, false),
    CLIENT_HELLO(0x67, false),
    LOGIN_PASSWORD(0x69, false),
    PLAYER_LOGGEDIN(0x6e, false),
    LOGIN_AUTHKEY(0x73, false),
    CLIENT_ERROR(0x85, false),
    CLIENT_FAIL(0x99, false),
    RSA_KEY(0xbc, false),
    CREATE_ULTIMATE(0x21),
    STRANGE_DATA(0x28),
    CHARLIST_REQUEST(0x6a),
    CHAR_SELECT(0x6f),
    CHECK_CHAR_NAME(0x74),
    SERVERLIST_REQUEST(0x75),
    CREATE_CHAR(0x7d),
    DELETE_CHAR(0x80),
    PART_TIME_JOB(0x8d),
    CHARACTER_CARDS(0x8e),
    REQUEST_CONNECTION(0x9c),
    CHAR_SELECTED(0xa5),
    UPDATE_CHARSLOT(0xa6),
    LIE_DETECTOR_RESPONSE(0xa7),
    LICENSE_REQUEST(0xa9),
    SET_GENDER(0xaa),
    SET_CHAR_CARDS(0xb4),
    SET_ACC_CASH(0xb5),
    QUICK_BUY_CS_ITEM(0xb6),
    SERVERSTATUS_REQUEST(0xbd),
    CREATE_CHAR_REQUEST(0xc0),
    SEAL_FAMILIAR(0xc1),
    BEGIN_USER(0xc7),
    CHANGE_MAP(0xc8),
    CHANGE_CHANNEL(0xc9),
    ENTER_CASH_SHOP(0xce),
    MOVE_PLAYER(0xd4),
    CANCEL_CHAIR(0xd5),
    USE_CHAIR(0xd6),
    CLOSE_RANGE_ATTACK(0xd9),
    RANGED_ATTACK(0xda),
    MAGIC_ATTACK(0xdb),
    PASSIVE_ENERGY(0xdc),
    UNK004B(0x7ffe),
    CLUSTER_ATTACK(0xdd),
    SPOTLIGHT_ATTACK(0xde),
    TAKE_DAMAGE(0xe0),
    GENERAL_CHAT(0xe1),
    CLOSE_CHALKBOARD(0xe2),
    FACE_EXPRESSION(0xe3),
    FACE_ANDROID(0xe4),
    USE_ITEM_EFFECT(0xe5),
    WHEEL_OF_FORTUNE(0xe6),
    USE_TITLE_EFFECT(0xe7),
    USE_ACTIVATE_DAMAGE_SKIN(0xe8),
    USE_ACTIVATE_DAMAGE_SKIN_PREMIUM(0xe9),
    USE_SAVE_DAMAGE_SKIN(0xea),
    NPC_TALK(0xf1),
    REMOTE_STORE(0xf2),
    NPC_TALK_MORE(0xf3),
    NPC_SHOP(0xf4),
    STORAGE(0xf5),
    USE_HIRED_MERCHANT(0xf6),
    MERCH_ITEM_STORE(0xf7),
    DUEY_ACTION(0xf8),
    MECH_CANCEL(0xf9),
    EXTRA_ATTACK(0xfb),
    SHOW_FIREWALL_REQUEST(0xfc),
    USE_HOLY_FOUNTAIN(0xfd),
    OWL(0xff),
    OWL_WARP(0x100),
    ITEM_GATHER(0x103),
    ITEM_SORT(0x104),
    ITEM_MOVE(0x105),
    MOVE_BAG(0x106),
    SWITCH_BAG(0x107),
    USE_ITEM(0x10a),
    CANCEL_ITEM_EFFECT(0x10b),
    USE_SUMMON_BAG(0x10d),
    PET_FOOD(0x10e),
    USE_MOUNT_FOOD(0x10f),
    USE_SCRIPTED_NPC_ITEM(0x110),
    USE_RECIPE(0x111),
    USE_CASH_ITEM(0x112),
    USE_ADDITIONAL_ADD_ITEM(0x113),
    ALLOW_PET_LOOT(0x114),
    ALLOW_PET_AOTO_EAT(0x115),
    USE_OPTION_CHANGE_REQUEST(0x116),
    USE_CATCH_ITEM(0x118),
    USE_SKILL_BOOK(0x119),
    USE_SP_RESET(0x11a),
    USE_AP_RESET(0x11b),
    USE_OWL_MINERVA(0x123),
    USE_TELE_ROCK(0x124),
    USE_RETURN_SCROLL(0x125),
    USE_UPGRADE_SCROLL(0x126),
    USE_FLAG_SCROLL(0x127),
    USE_EQUIP_SCROLL(0x128),
    USE_EXITEM_UPGRADE(0x129),
    USE_POTENTIAL_SCROLL(0x12c),
    USE_POTENTIAL_ADD_SCROLL(0x12d),
    USE_ADDITIONAL_ITEM(0x12e),
    USE_WEAPON_TEMP_OPTION(0x12f),
    USE_SOULS_SCROLL(0x130),
    USE_SOUL_MARBLE(0x131),
    USE_MIRACLE_CUBE(0x132),
    USE_ENCHANTING(0x133),
    USE_ARCANE_FORCE_REQUEST(0x136),
    USE_BAG(0x138),
    USE_MAGNIFY_GLASS(0x139),
    USE_CRAFTED_CUBE(0x13a),
    TOADS_HAMMER_REQUEST(0x13d),
    TOADS_HAMMER_HELP_REQUEST(0x13e),
    DISTRIBUTE_AP(0x141),
    AUTO_ASSIGN_AP(0x142),
    HEAL_OVER_TIME(0x144),
    DEL_TEACH_SKILL(0x147),
    SET_TEACH_SKILL(0x148),
    DISTRIBUTE_SP(0x149),
    SPECIAL_MOVE(0x14a),
    CANCEL_BUFF(0x14b),
    SKILL_EFFECT(0x14d),
    MESO_DROP(0x7ffe),
    SUPER_CANNON_REQUEST(0x14f),
    GIVE_FAME(0x151),
    CHAR_INFO_REQUEST(0x153),
    SPAWN_PET(0x154),
    PET_AUTO_BUFF(0x155),
    CANCEL_DEBUFF(0x156),
    CHANGE_MAP_SPECIAL(0x157),
    USE_INNER_PORTAL(0x159),
    TROCK_ADD_MAP(0x15b),
    LIE_DETECTOR(0x15c),
    LIE_DETECTOR_SKILL(0x15d),
    LIE_DETECTOR_REFRESH(0x15e),
    REPORT(0x15f),
    QUEST_ACTION(0x161),
    REISSUE_MEDAL(0x162),
    MOVE_ENERGY(0x164),
    SPECIAL_ATTACK(0x165),
    SKILL_MACRO(0x169),
    REWARD_ITEM(0x16b),
    ITEM_MAKER(0x16c),
    REPAIR_ALL(0x16f),
    REPAIR(0x170),
    SOLOMON(0x7ffe),
    GACH_EXP(0x7ffe),
    FOLLOW_REQUEST(0x173),
    FOLLOW_REPLY(0x174),
    AUTO_FOLLOW_REPLY(0x177),
    PROFESSION_INFO(0x178),
    USE_POT(0x179),
    CLEAR_POT(0x17a),
    FEED_POT(0x17b),
    CURE_POT(0x17c),
    REWARD_POT(0x17d),
    USE_COSMETIC(0x17f),
    FORCEATOM_COLLISION(0x181),
    USER_TRUMP_SKILL_ACTION_REQUEST(0x183),
    DOT_HEAL_HP_REQUEST(0x184),
    USE_REDUCER(0x187),
    USE_REDUCER_PRESTIGE(0x188),
    PVP_RESPAWN(0x7ffe),
    USER_DAZZLE_HIT(0x192),
    CHANGE_ZERO_LOOK(0x196),
    ZERO_SHARE_CASH_EQUIP_PART(0x197),
    CHANGE_ZERO_LOOK_END(0x198),
    PARTYCHAT(0x1a2),
    WHISPER(0x1a4),
    MESSENGER(0x1a5),
    PLAYER_INTERACTION(0x1a6),
    PARTY_OPERATION(0x1a8),
    DENY_PARTY_REQUEST(0x1a9),
    ALLOW_PARTY_INVITE(0x1aa),
    EXPEDITION_OPERATION(0x1ab),
    EXPEDITION_LISTING(0x1ac),
    GUILD_OPERATION(0x1ae),
    DENY_GUILD_REQUEST(0x1af),
    GUILD_APPLY(0x1b0),
    ACCEPT_GUILD_APPLY(0x1b2),
    DENY_GUILD_APPLY(0x1b3),
    TOWER_RANK_REQUEST(0x1b5),
    ADMIN_COMMAND(0x1b6),
    BUDDYLIST_MODIFY(0x1b8),
    NOTE_ACTION(0x1b9),
    RPS_GAME(0x1bc),
    USE_DOOR(0x1bd),
    USE_MECH_DOOR(0x1bf),
    CHANGE_KEYMAP(0x1c1),
    RING_ACTION(0x1c6),
    ARAN_COMBO(0x1d0),
    LOST_ARAN_COMBO(0x1d1),
    OVERLOAD_MODE_RESULT(0x1d5),
    CRAFT_DONE(0x1d9),
    CRAFT_EFFECT(0x1da),
    CRAFT_MAKE(0x1db),
    MICRO_BUFF_END_TIME(0x1de),
    CHANGE_MARKET_MAP(0x1e0),
    MEMORY_SKILL_CHOOSE(0x1e1),
    MEMORY_SKILL_CHANGE(0x1e2),
    MEMORY_SKILL_OBTAIN(0x1e3),
    BUY_CROSS_ITEM(0x1ec),
    USE_TEMPEST_BLADES(0x1ee),
    DISTRIBUTE_HYPER_SP(0x1f5),
    RESET_HYPER_SP(0x1f6),
    DISTRIBUTE_HYPER_AP(0x1f7),
    RESET_HYPER_AP(0x1f8),
    CHANGE_PLAYER(0x207),
    UNKNOWN_168(0x209),
    USER_HOWLING_STORM_STACK(0x218),
    USER_COMPLETE_NPC_SPEECH(0x21c),
    COMBO_CHECK_REQUEST(0x21e),
    ADD_ATTACK_RESET(0x22a),
    FAMILIAR_OPERATION(0x238),
    SOUL_MODE(0x236),
    USE_TOWERCHAIR_SETTING(0x23e),
    OPEN_MAPLE_UNION_REQUEST(0x243),
    VMATRIX_MAKE_REQUEST(0x251),
    VMATRIX_HELP_REQUEST(0x252),
    EVENT_REVIVE_REQUEST(0x255),
    EXCHANGE_POSITION_REQUEST(0x256),
    FORCE_TARGET_REQUEST(0x258),
    CRYSTAL_CHARGE(0x25a),
    MOVE_PET(0x262),
    PET_CHAT(0x263),
    PET_COMMAND(0x264),
    PET_LOOT(0x265),
    PET_AUTO_POT(0x266),
    PET_EXCEPTION_LIST(0x267),
    PET_AOTO_EAT(0x268),
    MOVE_LITTLEWHITE(0x26c),
    MOVE_SUMMON(0x273),
    SUMMON_ATTACK(0x274),
    DAMAGE_SUMMON(0x275),
    SUB_SUMMON(0x276),
    SUMMON_MAGIC_ALTAR(0x277),
    SUMMON_REMOVE(0x278),
    SUMMON_ACTION(0x279),
    SUMMON_ASSIST_ATTACK_DONE(0x27a),
    SUMMON_SARAH_ACTION(0x27b),
    SUMMON_JAVELIN_ACTION(0x27d),
    MOVE_DRAGON(0x281),
    DRAGON_FLY(0x282),
    MOVE_ANDROID(0x285),
    SUB_LITTLEWHITE(0x28a),
    QUICK_SLOT(0x28c),
    PASSIVE_SKILL_INFO_UPDATE(0x28d),
    PLAYER_VIEW_RANGE(0x290),
    SYSTEM_PROCESS_LIST(0x293),
    CHANGE_POTENTIAL_BOX(0x295),
    CHANGE_POTENTIAL_WP(0x296),
    CHANGE_POTENTIAL(0x297),
    SHOW_LOVE_RANK(0x299),
    SPAWN_ARROWS_TURRET(0x2a3),
    USE_GROWTH_HELPER_REQUEST(0x2b9),
    WARLOCK_MAGIC_ATTACK(0x2bf),
    RUN_SCRIPT(0x2c0),
    ENTER_STARTPLANET(0x7ffe),
    CLIENT_CLOSE(0x2ef),
    TRACK_FLAMES(0x2f0),
    SELECT_JAGUAR(0x2f5),
    FPS_LOG(0x2f8),
    PACKET_MODIFY_LOG(0x2f9),
    GIVE_KSPSYCHIC(0x312),
    ATTACK_KSPSYCHIC(0x313),
    CANCEL_KSPSYCHIC(0x314),
    GIVE_KSULTIMATE(0x316),
    ATTACK_KSULTIMATE(0x317),
    MIST_KSULTIMAT(0x318),
    CANCEL_KSULTIMATE(0x319),
    PSYCHIC_OVER_REQUEST(0x31a),
    SIGNIN_OPERATION(0x324),
    RW_CLEAR_CURRENT_ATTACK_REQUEST(0x32c),
    RW_MULTI_CHARGE_CANCEL_REQUEST(0x32d),
    MULTI_SKILL_ATTACK_REQUEST(0x334),
    REMOTE_DICE_REQUEST(0x339),
    MULTI_SKILL_CHARGE_REQUEST(0x33a),
    GIVE_SPOTLIGHT_BUFF(0x33c),
    MAPLE_UNION_REQUEST(0x33e),
    ILLIUM_JAVE_LIN(0x34d),
    USE_NEBULITE(0x357),
    USE_ALIEN_SOCKET(0x7ffe),
    USE_ALIEN_SOCKET_RESPONSE(0x7ffe),
    USE_NEBULITE_FUSION(0x7ffe),
    POTION_POT_USE(0x35a),
    POTION_POT_ADD(0x35b),
    POTION_POT_MODE(0x35c),
    POTION_POT_INCR(0x35d),
    USE_SPECIAL_ITEM(0x365),
    APPLY_HYUNCUBE(0x36a),
    CALL_FRIENDS(0x36b),
    BBS_OPERATION(0x37b),
    SELECT_CHAIR(0x37f),
    TRANSFORM_PLAYER(0x383),
    OPEN_AVATAR_RANDOM_BOX(0x384),
    ENTER_MTS(0x385),
    FISHING(0x38a),
    USE_TREASUER_CHEST(0x38b),
    SHIKONGJUAN(0x394),
    SET_CHAR_CASH(0x399),
    OPEN_WORLDMAP(0x39c),
    SAVE_DAMSKIN(0x3a3),
    CHANGE_DAMSKIN(0x3a4),
    DELETE_DAMSKIN(0x3a5),
    MOVE_LIFE(0x3bf),
    AUTO_AGGRO(0x3c0),
    MONSTER_DROPPICKUP_REQUEST(0x3c1),
    MONSTER_HIT_BY_OBSTACLE(0x3c0),
    MONSTER_HIT_BY_OBSTACLE_ATOM(0x3c3),
    MONSTER_HIT_BY_MOB(0x3c4),
    MONSTER_BOMB(0x3c5),
    MONSTER_BOMB_COLLISION_GROUP(0x3c6),
    MONSTER_TIME_BOMB_END(0x3c9),
    MONSTER_ESCORT_COLLISION(0x3ca),
    MONSTER_REQUEST_ESCORT_INFO(0x3cb),
    MONSTER_ESCORT_STOP_END_REQUEST(0x3cc),
    MONSTER_SPECIAL_SKILL(0x3cd),
    NPC_ACTION(0x3dc),
    ITEM_PICKUP(0x3e2),
    DAMAGE_REACTOR(0x3e5),
    TOUCH_REACTOR(0x3e6),
    MAKE_EXTRACTOR(0x3f2),
    SNOW_BALL_HIT(0x3fa),
    CONTISTATE(0x407),
    PLAYER_UPDATE(0x40a),
    PARTY_SEARCH_START(0x40d),
    PARTY_SEARCH_STOP(0x40e),
    START_HARVEST(0x412),
    STOP_HARVEST(0x413),
    ACT_CHANGE_REACTOR_USEREQUST(0x414),
    QUICK_MOVE(0x416),
    USE_RUNE(0x417),
    USE_RUNE_SKILL_REQ(0x418),
    OBTACLE_ATOM_COLLISION(0x419),
    FPS_MODE_EXIT_REQUEST(0x41d),
    MakeEnterFieldPacketForMirrorDungeon(0x420),
    CRYSTAL_CONNECT(0x424),
    DEMIANOBJECT_MAKE_ENTER_ACK(0x49b),
    DEMIANOBJECT_NODE_END(0x49c),
    DEMIANOBJECT_ERR_RECREATE(0x49d),
    CS_UPDATE(0x4e9),
    BUY_CS_ITEM(0x4ea),
    COUPON_CODE(0x4eb),
    SEND_CS_GIFI(0x4ed),
    USE_HAMMER(0x50a),
    HAMMER_RESPONSE(0x50c),
    BATTLE_STATISTICS(0x50f),
    HIDDEN_TAIL_ADN_EAR(0x7ffe),
    PAM_SONG(0x7ffe),
    TAP_JOY_RESPONSE(0x526),
    TAP_JOY_DONE(0x527),
    TAP_JOY_NEXT_STAGE(0x528),
    EFFECT_SWITCH(0x532),
    MAX_COUNT(0x539),

    UNKNOWN;

    static {
        reloadValues();
    }

    private int code = -2;
    private boolean CheckState;

    RecvPacketOpcode() {
        this.CheckState = true;
    }

    RecvPacketOpcode(int code) {
        this.code = code;
    }

    RecvPacketOpcode(int code, boolean CheckState) {
        this.CheckState = CheckState;
        this.code = code;

    }

    public static String getNamebyID(int val) {
        for (RecvPacketOpcode op : RecvPacketOpcode.values()) {
            if (op.getValue() == val) {
                return op.name();
            }
        }
        return "UNKNOWN";
    }

    public static RecvPacketOpcode getByType(int type) {
        for (RecvPacketOpcode l : RecvPacketOpcode.values()) {
            if (l.getValue() == type) {
                return l;
            }
        }
        return UNKNOWN;
    }

    public static Properties getDefaultProperties() throws IOException {
        Properties props = new Properties();
        FileInputStream fileInputStream = new FileInputStream("properties/recvops.properties");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
        BufferedReader buff = new BufferedReader(inputStreamReader);
        props.load(buff);
        buff.close();
        inputStreamReader.close();
        fileInputStream.close();
        return props;
    }

    public static void reloadValues() {
        try {
            File file = new File("properties/recvops.properties");
            if (file.exists()) {
                ExternalCodeTableGetter.populateValues(getDefaultProperties(), values());
            }
        } catch (IOException e) {
            throw new RuntimeException("加载 recvops.properties 文件出现错误", e);
        }
    }

    @Override
    public short getValue() {
        return (short) code;
    }

    @Override
    public void setValue(short code) {
        this.code = code;
    }

    public boolean NeedsChecking() {
        return CheckState;
    }
}
